from matplotlib import pyplot as plt
from wordcloud import WordCloud


with open("三国人名汇总.txt", "r") as file:
    dic = dict(((name, 0) for name in file.read().split()))

with open("三国演义.txt", "r", encoding="ANSI") as file:
    for line in file:
        for word in dic:
            dic[word] += line.count(word)

greater_than_100 = [(k, v) for k, v in dic.items() if v > 100]


plt.subplot(121)
x = [k for k, _ in greater_than_100]
y = [v for _, v in greater_than_100]
hidden_x = list(range(0, len(greater_than_100)))
plt.bar(hidden_x, y)
plt.xticks(hidden_x, x, rotation=90)

plt.subplot(122)


def generate_words():
    for k, v in dic.items():
        for i in range(0, v):
            yield k


word_cloud = WordCloud(font_path='C:/Windows/Fonts/simhei.ttf', background_color='white')\
    .generate(" ".join(generate_words()))
plt.imshow(word_cloud)

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.show()
